package com.mdp.im.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mdp.im.core.entity.ImUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdp.core.service.BaseService;
import com.mdp.core.err.BizException;

import com.mdp.im.core.entity.ImPrichatMessage;
import com.mdp.im.core.mapper.ImPrichatMessageMapper;
/**
 * @author 唛盟开源 code-gen
 * @since 2024-8-13
 */
@Service
public class ImPrichatMessageService extends BaseService<ImPrichatMessageMapper,ImPrichatMessage> {

	@Autowired
	ImUserService imUserService;

	static Logger logger =LoggerFactory.getLogger(ImPrichatMessageService.class);

	/**
	 * 自定义查询，支持多表关联
	 * @param page 分页条件
	 * @param ew 一定要，并且必须加@Param("ew")注解
	 * @param ext 如果xml中需要根据某些值进行特殊处理，可以通过这个进行传递，非必须，注解也可以不加
	 * @return
	 */
	public List<Map<String,Object>> selectListMapByWhere(IPage page, QueryWrapper ew, Map<String,Object> ext){
		return baseMapper.selectListMapByWhere(page,ew,ext);
	}



	/**
	 * 查询最近5天的聊天记录所属的私人群组
	 * @param branchId
	 * @param userid
	 * @return
	 */
	public List<ImPrichatMessage> listMyPrichatGroups(String branchId, String userid){
		Map<String,Object> p=new HashMap<>();
		p.put("branchId", branchId);
		p.put("userid", userid);
		List<ImPrichatMessage> prichatMessages=  baseMapper.listMyPrichatGroups(p);
		return prichatMessages;

	}
	//重载
	public List<Map<String,Object>> listMyPrichatGroupsAndName(String branchId,String userid){
		Map<String,Object> p=new HashMap<>();
		p.put("branchId", branchId);
		p.put("userid", userid);
		List<Map<String,Object>> prichatMessages=  baseMapper.listMyPrichatGroupsAndName(p);
		return prichatMessages;
	}
	//重载
	public List<Map<String,Object>> listMyPrichatGroupsAndName(String branchId,String userid,String lastLoadImDataTime){
		Map<String,Object> p=new HashMap<>();
		p.put("branchId", branchId);
		p.put("userid", userid);
		p.put("lastLoadImDataTime",lastLoadImDataTime);
		List<Map<String,Object>> prichatMessages=  baseMapper.listMyPrichatGroupsAndName(p);
		return prichatMessages;
	}

	//设置为已读
	public void editSendToRead(ImPrichatMessage imPrichatMessage){
 		baseMapper.editSendToRead(imPrichatMessage);
	}


	//搜索申请好友的消息
	public List<Map<String,Object>> searchInviteJoinFrend( Map<String,Object> pa){
//		String id=(String)pa.get("");
//		Map<String,Object> p=new HashMap<>();
//		p.put("branchId", branchId);
//		p.put("userid", userid);
		List<Map<String,Object>> prichatMessages=  baseMapper.searchInviteJoinFrend(pa);
		return prichatMessages;
	}

	//搜索申请好友的消息
	public void addToFriend( Map<String,Object> pa){
		//搜索此电话是否注册了
		Map<String, Object> searchImUser = new HashMap<>();
		String phoneno=((String)pa.get("phoneno")).replaceAll(" ", "");;
		if(phoneno==null || "".equals(phoneno)){
			throw new BizException("联系人电话不能为空");
		}
		searchImUser.put("phoneno",phoneno);
		List<ImUser> imUserList = imUserService.listByMap(searchImUser);
		if(imUserList.size()<1){
			throw new BizException("该用户还没注册,无法添加");
		}
		for(ImUser imUser:imUserList){
			ImPrichatMessage imPrichatMessage=new ImPrichatMessage();
			imPrichatMessage.setMsgId(this.createKey("id"));
			imPrichatMessage.setSendUserid((String) pa.get("sendUserid"));
			imPrichatMessage.setSendTime(new Date());
			imPrichatMessage.setSendContent("邀请成为朋友");
			imPrichatMessage.setGroupId((String) pa.get("sendUserid")+"_"+imUser.getUserid());
			imPrichatMessage.setToUserid((String) imUser.getUserid());
			imPrichatMessage.setSendUsername((String) pa.get("sendUsername"));
			imPrichatMessage.setMsgAction("inviteJoinFrend");
			imPrichatMessage.setMsgState("send");
			this.insert(imPrichatMessage);
		}
	}

	public void editSendFrendToRead(Map<String, Object> mmap) {
		baseMapper.editSendFrendToRead(mmap);
	}
}

